<?php

namespace app\common\model;

use think\Db;
use think\Loader;
use think\Model;
use think\Validate;
use houdunwang\crypt;

/**
 *  Brand 品牌模型
 * Class Brand
 * @package app\common\model
 */
class Brand extends Model
{
    //类型tid
    protected $pk = 'bid';
    //获取当前的数据表
    protected $table = 'shop_brand';

    //类型首页
    public function lists()
    {
        return Db::table('shop_brand')->select();
    }

    //添加类型
    public function add($data)
    {
//        p($data);die;
        //执行添加 滤非数据表字段的数据
        $result = $this->validate(true)->save($data);
        //p($result);die; //false ，执行成功返回 1 为true  一下判断拿false做判断
        if (false === $result) {
            //错误信息
            return ['valid' => 0, 'msg' => $this->getError()];
        } else {
            //成功信息
            return ['valid' => 1, 'msg' => '添加成功'];
        }
    }

    //品牌的删除
    public function del($bid)
    {
        $result = Db::table('shop_brand')->where('bid', $bid)->delete();
        if (false === $result) {
            // 验证失败 输出错误信息
            return ['valid' => 0, 'msg' => $this->getError()];
            //dump($this->getError());
        } else {
            return ['valid' => 1, 'msg' => '删除成功'];
        }
    }
    //品牌的编辑
    public function edit($data)
    {
//        p($data);die;
        $result = $this->validate(true)->save($data,[$this->pk=>$data['bid']]);
//        p($result);die;
        if($result)
        {
            //执行成功
            return ['valid'=>1,'msg'=>'编辑成功'];
        }else{
            return ['valid'=>0,'msg'=>$this->getError()];
        }
    }

}
